﻿using UnityEngine;
using System.Collections;


public class BallCollectorScript : MonoBehaviour {

	public GameObject gc;
	public int aaa;

	private GameControllerScript gcs;

	// Use this for initialization
	void Start () {
		gcs = gc.GetComponent<GameControllerScript> ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void updateScores(){
		if (this.name == "LeftBallCollector") {
			gcs.rightScore += 1;
		}
		
		if (this.name == "RightBallCollector") {
			gcs.leftScore += 1;
		}
	}

	void flipDeathEmitter(ParticleSystem deathParticles){
		if (this.name == "LeftBallCollector") {
			deathParticles.transform.localEulerAngles = new Vector3(0, 90, 0);
		}
		
		if (this.name == "RightBallCollector") {
			deathParticles.transform.localEulerAngles = new Vector3(0, -90, 0);
		}
	}

	IEnumerator OnCollisionExit2D(Collision2D collision){
		if (collision.gameObject.tag == "Ball") {
			updateScores();
			BallScript ballScript = collision.gameObject.GetComponent<BallScript>();

			ballScript.trailParticles.emissionRate = (gcs.rightScore + gcs.leftScore) * 10;

			flipDeathEmitter(ballScript.deathParticles);
			ballScript.playDeath();

			yield return new WaitForSeconds(2.0f);

			ballScript.reset ();
			ballScript.trailParticles.Play();
		}
	}

}
